package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.member.WxOpenidRefEntity;
import com.dragon.modules.yuegou.enums.code.MemberCode;
import com.dragon.modules.yuegou.enums.member.WxOpenidRefOpenIdType;
import com.dragon.modules.yuegou.mapper.member.WxOpenidRefMapper;
import com.dragon.modules.yuegou.model.member.WxOpenidRef;
import com.dragon.modules.yuegou.query.member.WxOpenidRefQuery;
import com.dragon.modules.yuegou.service.member.WxOpenidRefService;
import com.dragon.modules.yuegou.web.Check;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 会员openid关系表 服务实现
 *
 * @author pbuilder generated
 * @date 2019-03-19 08:59:40
 * @version 2.0
 */
@Slf4j
@Service("memWxOpenidRefService")
public class WxOpenidRefServiceImpl
		extends ServiceImpl<WxOpenidRefMapper, WxOpenidRefEntity>
		implements WxOpenidRefService {

	@Override
	public void saveOrUpdate(String memberId, String openid, String unionid,String appid, WxOpenidRefOpenIdType openIdType) {
		if (StringUtils.isEmpty(openid) && (WxOpenidRefOpenIdType.ANDRIOD.getCode().equals(openIdType.getCode())
				|| WxOpenidRefOpenIdType.IOS.getCode().equals(openIdType.getCode()))){
			return;
		}

		Check.notNull(memberId, MemberCode.MEMBER_ID_IS_EMPTY.getMessage());
		Check.notNull(openIdType, "openid类型不能为空");
		Check.notNull(openid, "openid不能为空");
		log.debug("判断会员openid关系是否存在，会员id={}，openid={}，来源={}",memberId,openid,openIdType.getValue());
		//判断openid和unionid是否存在
		WxOpenidRefQuery query = new WxOpenidRefQuery();
		query.setOpenIdType(openIdType);
		query.setMemberId(memberId);
		query.setWxOpenId(openid);
		query.setAppid(appid);
		Long count = this.count(query.toQueryWrapper());
		//不存在插入一条记录
		if (count==null || count ==0){
			log.debug("判断会员openid关系是否存在，不存在对应关系，创建关系，会员id={}，openid={}，来源={}",memberId,openid,openIdType.getValue());
			WxOpenidRef entity = new WxOpenidRef();
			entity.setMemberId(memberId);
			entity.setWxOpenId(openid);
			entity.setWxUnionId(unionid);
			entity.setOpenIdType(openIdType);
			entity.setAppid(appid);
			this.save(entity);
		}
	}
	@Override
	public void saveOrUpdate(String memberId, String alipayAppid, String appid, WxOpenidRefOpenIdType openIdType) {
		Check.notNull(memberId, MemberCode.MEMBER_ID_IS_EMPTY.getMessage());
		Check.notNull(openIdType, "openid类型不能为空");
		log.debug("判断会员openid关系是否存在，会员id={}，openid={}，来源={}",memberId,alipayAppid,openIdType.getValue());
		//判断openid和unionid是否存在
		WxOpenidRefQuery query = new WxOpenidRefQuery();
		query.setOpenIdType(openIdType);
		query.setMemberId(memberId);
		query.setAliAppid(alipayAppid);
		query.setAppid(appid);
		Long count = this.count(query.toQueryWrapper());
		//不存在插入一条记录
		if (count==null || count ==0){
			log.debug("判断会员openid关系是否存在，不存在对应关系，创建关系，会员id={}，openid={}，来源={}",memberId,alipayAppid,openIdType.getValue());
			WxOpenidRef entity = new WxOpenidRef();
			entity.setMemberId(memberId);
			entity.setAliAppid(alipayAppid);
			entity.setOpenIdType(openIdType);
			entity.setAppid(appid);
			this.save(entity);
		}
	}
	@Override
	public void logout(String memberId,WxOpenidRefOpenIdType openIdType) {
		WxOpenidRefQuery query = new WxOpenidRefQuery();
		query.setOpenIdType(openIdType);
		query.setMemberId(memberId);
		remove(query.toQueryWrapper());
	}

	@Override
	public WxOpenidRef getBy(String memberId, String appid, WxOpenidRefOpenIdType openIdType) {
		WxOpenidRefQuery query = new WxOpenidRefQuery();
		query.setOpenIdType(openIdType);
		query.setMemberId(memberId);
		query.setAppid(appid);
		return getFirst(query);
	}
	
	@Override
	public WxOpenidRef getByAli(String aliAppid,String appId) {
		WxOpenidRefQuery query = new WxOpenidRefQuery();
		query.setOpenIdType(WxOpenidRefOpenIdType.ALIXCX);
		query.setAliAppid(aliAppid);
		query.setAppid(appId);
		QueryWrapper<WxOpenidRefEntity> wrapper = query.toQueryWrapper();
		wrapper.orderByDesc(WxOpenidRefEntity.COL_CREATED_TIME);

		List<WxOpenidRefEntity> refList = list(wrapper);
		if (CollectionUtils.isNotEmpty(refList)){
			return new WxOpenidRef(refList.get(0));
		}
		return null;
	}

	@Override
	public WxOpenidRef getFirst(WxOpenidRefQuery query){
		QueryWrapper<WxOpenidRefEntity> wrapper = query.toQueryWrapper();
		wrapper.orderByDesc(WxOpenidRefEntity.COL_CREATED_TIME);

		List<WxOpenidRefEntity> refList = list(wrapper);
		if (CollectionUtils.isNotEmpty(refList)){
			return new WxOpenidRef(refList.get(0));
		}
		return null;
	}
}